<?php

	/**
	 * SDB
	 * @author David Ivins
	 */
	class SDB
	{
		private $_service;
		
		/**
		 * __construct
		 */
		public function __construct($public_key, $private_key)
		{
			$config = Zend_Registry::get('config');

			require_once ($config->path->lib->amazon->file); 

			if ($config->aws->service == 'live')
				$this->_service = new Amazon_SimpleDB_Client($public_key, $private_key);
			else
				$this->_service = new Amazon_SimpleDB_Mock();
		}

		/* 
		// BEGIN UNUSED SECTION
		// createDomain 
		public function createDomain($name) 
		{
			$action = new Amazon_SimpleDB_Model_CreateDomain();
			$action->setDomainName($name); 

			$response = $this->_service->createDomain($action);
			return $response;
		}

		// listDomains
		public function listDomains() 
		{
			$action = new Amazon_SimpleDB_Model_ListDomains();
			$response = $this->_service->listDomains($action);
			return $response;
		}

		// deleteDomain
		public function deleteDomain($name) 
		{
			$action = new Amazon_SimpleDB_Model_DeleteDomain();
			$action->setDomainName($name); 

			$response = $this->_service->deleteDomain($action);
			return $response;
		}
		// END UNUSED SECTION 
		*/

		/**
		 * putAttributes
		 */
		function putAttributes($domain, $item, $attributes, $replace = true)
		{
			$attribute_list = array();

			foreach ($attributes as $key => $value)
			{
				$attribute = new Amazon_SimpleDB_Model_ReplaceableAttribute();
				$attribute->withName($key)->withValue($value)->withReplace($replace);
				$attribute_list[] = $attribute;
			}

			$action = new Amazon_SimpleDB_Model_PutAttributes();
			$action->withDomainName($domain)->withItemName($item)->setAttribute($attribute_list);
			$response = $this->_service->putAttributes($action);

			return $response;
		}
		
		/**
		 * getAttributes
		 */
		function getAttributes($domain, $item, array $attributes = null) 
		{
			$action = new Amazon_SimpleDB_Model_GetAttributes();
			$action->setDomainName($domain);
			$action->setItemName($item);
			
			if ($attributes != null)
			{
				foreach ($attributes as $attribute)
					$action->withAttributeName($attribute);
			}
			
			$response = $this->_service->getAttributes($action);
			return $response;
		}

		/**
		 * deleteAttributes
		 */
		function deleteAttributes($domain, $item, $attributes = null)
		{
			$action = new Amazon_SimpleDB_Model_DeleteAttributes();
			$action->setDomainName($domain);
			$action->setItemName($item);

			if ($attributes != null)
			{
				foreach ($attributes as $key => $value)
				{
					$attribute = new Amazon_SimpleDB_Model_Attribute();
						
					if ($value == null)
						$attribute->withName($key);
					else
						$attribute->withName($key)->withValue($value);
					$action->withAttribute($attribute);
				}
			}

			$response = $this->_service->deleteAttributes($action);
			return $response;
		}

		/**
		 * query
		 */
		function query($domain, $query_str, $max_items = null) 
		{
			$action = new Amazon_SimpleDB_Model_Query();
			$action->setDomainName($domain);
			$action->setQueryExpression($query_str);
			
			if (isset($max_items))
				$action->setMaxNumberOfItems($max_items);

			$response = $this->_service->query($action);
			return $response;
		}
	}
